//package com.authine.cloudpivot.web.api.advice;
//
//import com.alibaba.fastjson.JSONObject;
//import com.authine.cloudpivot.engine.domain.runtime.BizAttachment;
//import com.authine.cloudpivot.engine.repository.runtime.BizAttachmentRepository;
//import com.authine.cloudpivot.ext.controller.PocZcController;
//import com.authine.cloudpivot.ext.doman.BizAttachmentVo;
//import com.authine.cloudpivot.web.api.util.AuthUtils;
//import com.google.common.collect.Lists;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.CollectionUtils;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.jwt.Jwt;
//import org.springframework.security.jwt.JwtHelper;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.List;
//import java.util.Optional;
//import java.util.stream.Collectors;
//
///**
// * @Author hxd
// * @Date 2021/12/17 14:23
// * @Description
// **/
//@Slf4j
//@Aspect
//@Component
//public class AttachmentLoadAspect {
//
//
//    @Autowired
//    PocZcController pocZcController;
//
//
//    @Autowired
//    private BizAttachmentRepository bizAttachmentRepository;
//
//    /**
//     * Pointcut 切入点
//     *
//     */
//    @Pointcut("execution(* com.authine.cloudpivot.web.api.controller.common.OSSController.download(..))")
//    public void theAspect(){}
//    /**
//     * 环绕通知
//     */
////    @Around(value = "webLog()")
//    public Object arround(ProceedingJoinPoint pjp) {
//        try {
//            log.info("1、Around：方法环绕开始.....");
//            Object o =  pjp.proceed();
//            log.info("3、Around：方法环绕结束，结果是 :" + o);
//            return o;
//        } catch (Throwable e) {
//            log.error(pjp.getSignature() + " 出现异常： ", e);
//        }
//        return null;
//    }
//
//
//    private final static List<String> bizAttrPropertyCodeList = Lists.newArrayList("Attachment1639382526435","Attachment1639382546398","Attachment1640679461795");
//
//    /**
//     * 方法执行前
//     */
//    @Before(value = "theAspect()")
//    public void before(JoinPoint joinPoint){
////        log.info("2、Before：方法执行开始...");
////        // 接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
////        assert attributes != null;
//        HttpServletRequest request = attributes.getRequest();
////        // 记录下请求内容
////        log.info("URL : " + request.getRequestURL().toString());
////        log.info("HTTP_METHOD : " + request.getMethod());
////        log.info("IP : " + request.getRemoteAddr());
////        log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
////        log.info("=======================ARGS : " + Arrays.toString(joinPoint.getArgs()));
//
//        try {
//
//
//
//            String refId = request.getParameter("refId");
//
//
//            if (refId != null){
//
//                List<BizAttachmentVo> list =  getAttachmentByRefId(refId);
//                BizAttachmentVo attachment = null;
//                if (CollectionUtils.isNotEmpty(list)){
//                    Optional<BizAttachmentVo> first = list.stream().filter(a -> "kcqd".equals(a.getSchemaCode())).findFirst();
//                    if (first.isPresent()) {
//                        attachment = first.get();
//                    }
//                }
//
//
//                if (attachment != null && "kcqd".equals(attachment.getSchemaCode()) && bizAttrPropertyCodeList.contains(attachment.getBizPropertyCode()) ){
//                    log.info("下载量累增,attachment={}",JSONObject.toJSONString(attachment));
//                    if (attachment.getBizObjectId() != null ){
//                        log.info("\n=============下载量累增");
//                        //点击量增加
//                        pocZcController.courseStatistics(attachment.getBizObjectId(),2);
//                        // 加入我的学习
//                        String userId = AuthUtils.getUserId();
//                        if (userId == null) {
//                            String access_token = request.getParameter("T");
//                            Jwt decode = JwtHelper.decode(access_token);
//                            String claims = decode.getClaims();
//                            JSONObject jsonObject = JSONObject.parseObject(claims);
//                            userId = jsonObject.getString("user_id");
//                        }
//
//                        pocZcController.joinMyStudy(attachment.getBizObjectId(), userId);
//
//                    }else {
//                        log.info("\n=============下载量累增失败,数据不符合");
//                    }
//
//                }
//            }
//
//
//
//
//        } catch (Exception e) {
//            log.info("list 日志添加失败:{}", e);
//        }
//
//    }
//
//
//    public List<BizAttachmentVo> getAttachmentByRefId(String attachmentId) {
//
//        List<BizAttachment> attachmentList = bizAttachmentRepository.findAllByRefId(attachmentId);
//
//        if(CollectionUtils.isNotEmpty(attachmentList)){
//            List<BizAttachmentVo> collect = attachmentList.stream().map(bizAttachment -> {
//                BizAttachmentVo vo = new BizAttachmentVo();
//                BeanUtils.copyProperties(bizAttachment, vo);
//                return vo;
//            }).collect(Collectors.toList());
//
//            return collect;
//        }
//        return null;
//    }
//
//
//
//    private JSONObject getParam(Object[] args){
//        if (args != null) {
//            return (JSONObject) JSONObject.toJSON(args[0]);
//        }
//
//        return null;
//    }
//
//    /**
//     * 方法执行结束，不管是抛出异常或者正常退出都会执行
//     */
////    @After(value = "listAspect()")
//    public void after(JoinPoint joinPoint){
//
//
//    }
//
//    /**
//     * 方法执行结束，增强处理
//     */
////    @AfterReturning(returning = "ret", pointcut = "webLog()")
//    public void doAfterReturning(Object ret){
//        // 处理完请求，返回内容
//
//    }
//
//    /**
//     * 后置异常通知
//     */
////    @AfterThrowing(value = "webLog()")
//    public void throwss(JoinPoint joinPoint){
//
//    }
//}
